﻿/*******************************************************************************
#                    BulletinBroker.cs
#  Comment:
#
#  Current Version: V1.0
#  Author: 
#
#  History List:
#  V1.0    Created by Onions@2009-8-18 20:07:18
#
#******************************************************************************/
#region Usings
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using SYS.Basic;

using ExpertMIS.Brokers;
using ExpertMIS.Entities;
using System.Collections;
#endregion

namespace ExpertMIS.Brokers
{
    public class BulletinBroker : ExpertMISBaseBroker
    {
        #region Private Methods

        private object UpdateBulletinEntity(IEntity entity)
        {
            return SqlMap.Update("UpdateBulletinEntity", entity);
        }

        private object InsertBulletinEntity(IEntity entity)
        {
            return SqlMap.Insert("InsertBulletinEntity", entity);
        }

        private object DeleteBulletinEntity(IEntity entity)
        {
            return SqlMap.Delete("DeleteBulletinEntity", entity);
        }

        #endregion

        #region Override Methods

        protected override IEntity ProcessGetEntity(System.Collections.Hashtable keys)
        {
            if (!keys.Contains("OID")) throw new Exception("invalid request parameters.");
            return SqlMap.QueryForObject<IEntity>("SelectBulletinEntityByOID", keys["OID"] as string);
        }

        protected override bool ProcessSaveEntity(IEntity entity, Hashtable request)
        {
            try
            {
                if (entity.EntityState == EntityState.New)
                {
                    InsertBulletinEntity(entity);
                    entity.EntityState = EntityState.Old;
                }
                else
                {
                    UpdateBulletinEntity(entity);
                }
            }
            catch (Exception ex)
            {
                Logger.Error(GetType().ToString() + ".ProcessSaveEntity", ex);
            }

            return true;
        }

        protected override IEntity ProcessNewEntity()
        {
            BulletinEntity entity = new BulletinEntity();

            // initial entity object

            return entity;
        }

        protected override bool ProcessRemoveEntity(IEntity entity, Hashtable request)
        {
            try
            {
                DeleteBulletinEntity(entity);
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region Public Method
        public IList<BulletinEntity> GetTopNBulletin(int n)
        {
            return SqlMap.QueryForList<BulletinEntity>("SelectTopNBulletin", n);
        }

        /// <summary>
        /// 获取人才为社会服务首页公告列表
        /// </summary>
        /// <returns></returns>
        public IList<BulletinEntity> GetSocialBulletins(int n) 
        {
            return SqlMap.QueryForList<BulletinEntity>("SelectSocialBulletins", n);
        }

        /// <summary>
        /// 获取组织为人才服务首页公告列表
        /// </summary>
        /// <returns></returns>
        public IList<BulletinEntity> GetOrganizationBulletins(int n)
        {
            return SqlMap.QueryForList<BulletinEntity>("SelectOrganizationBulletins", n);
        }

        public BulletinEntity GetBulletinByOID(string BulletinOID)
        {
            Hashtable htKeys = new Hashtable();
            htKeys["OID"] = BulletinOID;

            return GetEntity(htKeys) as BulletinEntity;
        }

        #endregion
    }
}